/**
 * Created by wxf on 2020/9/11.
 */
$(function () {
    // 1.获取参数
    var queryWidgets = getQueryString("queryWidgets");
    //开始月份、结束月份、项目名称
    // var arr = [{"id":2939,"value":"2020-08"},{"id":2940,"value":"2020-09"},{"id":2926,"value":""}];
    var arr = JSON.parse(queryWidgets);
    var beginMonth="",endMonth="",xmmc="";
    console.log(arr);
    if(arr){
        for(var i=0;i<arr.length;i++){
            var id = arr[i].id,value = arr[i].value;
            switch(id){
                case 2831:
                    beginMonth=value;
                    break;
                case 2832:
                    endMonth=value;
                    break;
                case 2836:
                    xmmc=value;
            }
        }
    }
    var selectedPjData={};
    //项目列表
    layui.use('table', function(){
        var $ = layui.$;
        var table = layui.table;
        var tableRender = table.render({
            elem: '#table'
            ,height:300
            ,even: true //开启隔行背景
            ,url:$ctx+'/pjProjectInfo/queryBuildingByMonthAndName'
            ,where: {beginMonth:beginMonth, endMonth: endMonth,pjName:xmmc}
            ,cols: [[
                {field:'id', title: 'ID', hide: true}
                ,{field:'procode', title: '项目编码',width:150}
                ,{field:'name', title: '项目名称',width:150,sort:true}
                ,{field:'prjtype', title: '项目类型',width:90}
                ,{field:'xmjd', title: '项目阶段',width:90, hide: true} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增,
                ,{field:'dateDay', title: '项目日期',hide: true}
                ,{field:'khmc', title: '客户名称',width:130}
                ,{field:'cpmc', title: '产品名称',width:150}
                ,{field:'htje', title: '合同总额',width:90}
                ,{field:'passnoworktime', title: '施工期限',width:90}
                ,{field:'xsjl', title: '销售经理',width:90}
                ,{field:'xmjl', title: '项目经理',width:90}
                ,{field:'ywjl', title: '运维经理',width:90}
                ,{field:'members', title: '项目成员',width:120}
                ,{field:'companyName', title: '所属公司',width:90}
                ,{field:'ssbm', title: '所属部门',width:90}
                ,{field:'ssjyt', title: '所属经营体',width:100}
                ,{field:'status', title: '项目状态',width:90, hide: true}
                ,{field:'sgazfyzj', title: '施工预算',hide:true}
                ,{field:'htjeCg', title: '采购总价',hide:true}
            ]]
        });
        //重载
        var active = {
            reload: function(){
                //执行重载
                table.reload('table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        beginMonth:beginMonth,
                        endMonth: endMonth,
                        pjName:xmmc
                    }
                }, 'data');
            }
        };

        //监听行单击事件
        table.on('row(layui-table)', function(obj){
            selectedPjData=obj.data;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            $("#xmxq").html(obj.data.name+"-项目详情")
            queryGantt(selectedPjData);
            //查询项目KPI指标值
            getKpiSpanData(selectedPjData);
        });
    });
    // $("#ganttChart").ganttView("setSlideWidth", 600);

    $("#ganttChart").css("height",$("#kpi-table").height());
    // 根据项目编码查询甘特图数据
    queryGantt(selectedPjData);
});

//查询项目KPI指标值
function getKpiSpanData(selectedPjData) {
    $("#htje").html(selectedPjData.htje===null?"0万":(parseFloat(selectedPjData.htje)/10000).toFixed(2)+"万");
    $("#sgys").html(selectedPjData.sgazfyzj===null?"0万":(parseFloat(selectedPjData.sgazfyzj)/10000).toFixed(2)+"万");
    $("#cgzj").html(selectedPjData.htjeCg===null?"0万":(parseFloat(selectedPjData.htjeCg)/10000).toFixed(2)+"万");
    var xmbm=selectedPjData.procode;
    $.ajax({
        url: $ctx+'/pjProjectBuilding/getKpiByXmbm',
        // dataType: 'json',
        data:{
            "xmbm":xmbm
        },
        type: 'get',
        success: function (data) {
            var res=data.data;
            if(res){
                var statusYwts=res["statusYwts"];
                for(var i=0;i<statusYwts.length;i++){
                    switch (statusYwts[i].mxa_sfyxgq){
                        case "设备采购":
                            $("#sbjcywts").html(statusYwts[i].mxa_yxts);
                            break;
                        case "施工队伍进场":
                            $("#sgdwjcyw").html(statusYwts[i].mxa_yxts);
                            break;
                        case "施工手续办理":
                            $("#sgsxblyw").html(statusYwts[i].mxa_yxts);
                            break;
                    }
                }
                var sjgq=res["sjgq"];
                $("#xmljgq").html(sjgq?sjgq.sjgq:0);
            }
        }
    });
}

// 根据项目编码查询甘特图数据
function queryGantt(selectedPjData){
    if(selectedPjData.procode){
        // 2.根据参数查询数据
        $.ajax({
            url: $ctx+'/pjProjectBuilding/getByXmbm',
            // dataType: 'json',
            data:{
                "xmbm":selectedPjData.procode
            },
            type: 'get',
            success: function (data) {
                var res=data.data;
                if(data.code == "200" && res.length > 0){
                    for (var i = 0; i < res.length; i++){
                        for (var j = 0; j < res[i].series.length; j++){
                            res[i].series[j].start = new Date(res[i].series[j].start);
                            res[i].series[j].end = new Date(res[i].series[j].end);
                        }
                    }
                    // 显示查询到的数据
                    $("#ganttChart").ganttView({
                        ganttScale: "months",
                        data: res,
                        slideWidth: 900,
                        behavior: {
                            clickable: false,
                            draggable: false,
                            resizable: false
                        }
                    });
                } else {
                    // 没查到先显示默认数据,这里后面要去掉，演示用
                    $("#ganttChart").ganttView({
                        ganttScale: "months",
                        data: ganttData,
                        slideWidth: 900,
                        behavior: {
                            clickable: false,
                            draggable: false,
                            resizable: false
                        }
                    });
                }
            }
        });
    }
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURIComponent(decodeURIComponent(window.location.search)).substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

